#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

/*把字符串转换为整数*/
int char_to_int(char* a)
{
	int n,ret,i;
	ret = 0;
	n = strlen(a);

	for(i = n-1;i >= 0;i--)
	{
		if(*(a+i) == '1')
			ret = ret + pow(2,n-i-1);
	}

	return ret;
}

char* addBinary(char* a,char* b)
{
	char* sum;
	int n = 1;//二进制数的位数
	int result = char_to_int(a) + char_to_int(b);

	/*计算和的位数*/
	while(1)
	{
		result = result/2;
		if(result == 0)	break;
		n++;
	}
	sum = "1";

	/*把和转换为字符串*/
	result = char_to_int(a) + char_to_int(b);
	while(1)
	{
		if(n < 2)	break;
		if((1<<n-2) & result)//如果结果的第n-2位是1
			sum = strcat(sum,"1");
		else
			sum = strcat(sum,"0");
	}

	return sum;
}


